Bitwise OR Operator (|)
The | Operator takes two equivalent length bit designs as boundaries; if the two bits in the looked-at position are 0, the next bit is zero. If not, it is 1.
Example:
Take two bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 . Take Bitwise OR of both X, y
Explanation: On the basis of truth table of bitwise OR operator we can conclude that the result of
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0We used the similar concept of bitwise operator that are show in the image.
Implementation of OR operator:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 12, b = 25;
int result = a | b;
cout << result;
return 0;
}
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 12, b = 25;
int result = a | b;
System.out.println(result);
}
}
a = 12
b = 25
result = a | b
print(result)
# This code is contributed by garg28harsh.
using System;
public class GFG{
static public void Main (){
int a = 12, b = 25;
int result = a | b;
Console.WriteLine(result);
}
}
// This code is contributed by akashish__
let a = 12, b = 25;
let result = a | b;
document.write(result);
// This code is contributed by garg28harsh.
Output
29
Time Complexity: O(1)
Auxiliary Space: O(1)
Introduction to Bitwise Algorithms – Data Structures and Algorithms Tutorial
Bit stands for binary digit. A bit is the basic unit of information and can only have one of two possible values that is 0 or 1. In our world, we usually with numbers using the decimal base. In other words. we use the digit 0 to 9 However, there are other number representations that can be quite useful such as the binary number systems.
Unlike humans, computers have no concepts of words and numbers. They receive data encoded at the lowest level as a series of zeros and ones (0 and 1). These are called bits, and they are the basis for all the commands they receive. We’ll begin by learning about bits and then explore a few algorithms for manipulating bits. We’ll then explore a few algorithms for manipulating bits. The tutorial is meant to be an introduction to bit algorithms for programmers.
Table of Content
- What is Bitwise Algorithms?
- Bitwise Operators / Basics of Bit manipulation
- Bitwise AND Operator (&)
- Bitwise OR Operator (|)
- Bitwise XOR Operator (^)
- Bitwise NOT Operator (!~)
- Left-Shift (<<)
- Right-Shift (>>)
- Application of Bit Operators
- Important Practice Problems on Bitwise Algorithm